package com.meiqiongproperty.yiguanjia.fragment.user;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.activity.MainActivity;
import com.meiqiongproperty.yiguanjia.commom.ViewConstant;
import com.meiqiongproperty.yiguanjia.databinding.TelephoneloginLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.Advertising;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendLogin;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendMsg;
import com.meiqiongproperty.yiguanjia.httpbean.HomeNoticeBean;
import com.meiqiongproperty.yiguanjia.httpbean.UserInfo;
import com.meiqiongproperty.yiguanjia.httpbean.user.SmSCode;
import com.meiqiongproperty.yiguanjia.utils.CountDownTimerUtils;
import com.meiqiongproperty.yiguanjia.utils.DeviceUtil;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.List;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.user
 * @User ChenJing
 * @Date 2017/4/25
 */

public class TelephoneLoginFragment extends BaseFragment {
    TelephoneloginLayoutBinding binding;
    private String phone, verify_code;
    Handler handler = new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.telephonelogin_layout, container, false);
        binding.setClick(this);
        return binding.getRoot();
    }

    public void getCode_Click(View view) {
        if (isClikVualuePhone()) {
            sendMsg();
        }
    }

    private void sendMsg() {
        //type 登录：LOGIN 注册：REGISTER 找回密码：RESETPASSWORD
        SendMsg sendmsg = new SendMsg();
        sendmsg.setMobile(phone);
        sendmsg.setType("LOGIN");
        ApiService.sendMsg(getActivity(), new SimpleHttpCallback<SmSCode>() {
            @Override
            public void onNext(SmSCode smsCode) {
                LogUtil.d("smsCode=" + smsCode.getCode());
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(binding.getVerifyCodeBtn, 60000, 1000);
                mCountDownTimerUtils.start();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, Utils.getRequestBody(sendmsg));
    }

    public boolean isClikVualue() {
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getActivity(), "请输入手机号", Toast.LENGTH_LONG).show();
            return false;
        }
        verify_code = binding.verifyCode.getEditableText().toString();
        if (TextUtils.isEmpty(verify_code)) {
            Toast.makeText(getActivity(), "请输入验证码", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    public boolean isClikVualuePhone() {
        phone = binding.phone.getEditableText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getActivity(), "请输入手机号", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    public void commitClick(View view) {
        if (isClikVualue()) {
            doLogin();
        }
    }

    private void doLogin() {
        SendLogin sendLogin = new SendLogin(phone, DeviceUtil.getIMEI(getActivity()), verify_code);
        ApiService.doLogin(getActivity(), new SimpleHttpCallback<UserInfo>() {
            @Override
            public void onNext(final UserInfo userinfo) {
                LogUtil.d("doLogin=" + userinfo.getUserinfo().toString());
                DBUserUtils.initUserInfoDaoAndSetUser(getActivity(), userinfo);
//                DBUserUtils.initHouseDao(getActivity(),userinfo.getUserinfo().getDefaultHouse());

                getNoticeInfo(userinfo.getUserinfo().getToken(), userinfo.getDefaultHouse().getID());
//                nextStep(userinfo);
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
//                Utils.ShowMsg(message, getActivity());
            }
        }, Utils.getRequestBody(sendLogin));
    }

    public void ToForgotpassword(View view) {
        GenericFragmentActivity.start(getActivity(), ForgotPasswordFragment.class, new Bundle());
    }

    public void ToRegister(View view) {
        GenericFragmentActivity.start(getActivity(), RegisterFragment.class, new Bundle());
    }


    /**
     * 获取公告信息
     */
    public void getNoticeInfo(String token, String roomId) {
        Advertising advertising = new Advertising();
        advertising.setRoomId(roomId);
        advertising.setToken(token);
        advertising.setType("1");
        ApiService.getHomeNotice(getActivity(), new IHttpCallback<HomeNoticeBean>() {
            @Override
            public void onNext(HomeNoticeBean homeNoticeBean) {
                ViewConstant.notice.clear();
                List<HomeNoticeBean.NoticeBean> list_notice = homeNoticeBean.getNotice();
                for (int i = 0; i < list_notice.size(); i++) {
                    String title = list_notice.get(i).getTitle();
                    ViewConstant.notice.add(title);
                }
                LogUtil.d("notice===========>" + ViewConstant.notice.size());
                Intent intent;
                intent = new Intent(getContext(),
                        MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(advertising));
    }
}
